<HTML>
 
<!-- Mirrored from www.javapractices.com/apps/movies/javadoc/src-html/hirondelle/movies/exception/ExceptionHandler.html by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:30:18 GMT -->
<HEAD>
  <TITLE>
ExceptionHandler.java
  </TITLE>
  <LINK REL ='stylesheet' TYPE='text/css' HREF='../../../../highlight.css' TITLE='Style'>
 </HEAD>
 <BODY>
<PRE>
<span class='keyword'>package</span> hirondelle.movies.exception;<a name=line.1></a>
<a name=line.2></a>
<span class='keyword'>import</span> hirondelle.movies.util.Util;<a name=line.3></a>
<span class='keyword'>import</span> hirondelle.movies.util.ui.UiUtil;<a name=line.4></a>
<a name=line.5></a>
<span class='keyword'>import</span> java.io.PrintWriter;<a name=line.6></a>
<span class='keyword'>import</span> java.io.StringWriter;<a name=line.7></a>
<span class='keyword'>import</span> java.io.Writer;<a name=line.8></a>
<span class='keyword'>import</span> java.util.logging.Logger;<a name=line.9></a>
<span class='keyword'>import</span> javax.swing.JOptionPane;<a name=line.10></a>
<a name=line.11></a>
<span class='comment'>/**<a name=line.12></a>
 Custom handler for uncaught exceptions.<a name=line.13></a>
 <a name=line.14></a>
 &lt;P&gt;By default, a Swing app will handle uncaught exceptions simply by printing a stack trace to <a name=line.15></a>
 {@link System#err}. However, the end user sees nothing happen. <a name=line.16></a>
  This class addresses that problem, by showing the end user a simple error message in a modal dialog.<a name=line.17></a>
  (The dialog's owner is the currently active frame.)<a name=line.18></a>
  <a name=line.19></a>
 &lt;P&gt;In JDK1.4, the simplest way of overriding the default handler for uncaught exceptions is  <a name=line.20></a>
 to use the following undocumented feature :<a name=line.21></a>
 &lt;ul&gt;<a name=line.22></a>
   &lt;li&gt;create a class with a no-argument constructor<a name=line.23></a>
   &lt;li&gt;add to it a method :&lt;tt&gt;public void handle(Throwable aThrowable){...}&lt;/tt&gt;<a name=line.24></a>
   &lt;li&gt;upon startup, add a {@link System} property named &lt;tt&gt;'sun.awt.exception.handler'&lt;/tt&gt;, <a name=line.25></a>
   whose value is the fully qualified class name of this new handler class<a name=line.26></a>
   &lt;/ul&gt; <a name=line.27></a>
 <a name=line.28></a>
 The above technique is used by this class. <a name=line.29></a>
 <a name=line.30></a>
 &lt;P&gt;Other alternative methods include :<a name=line.31></a>
 &lt;ul&gt;<a name=line.32></a>
   &lt;li&gt;create a custom {@link java.util.logging.Handler}, and attach it to your application's loggers<a name=line.33></a>
   &lt;li&gt;override {@link ThreadGroup#uncaughtException(java.lang.Thread, java.lang.Throwable)}<a name=line.34></a>
   &lt;li&gt;use later versions of Java, which provides an API related to this problem<a name=line.35></a>
  &lt;/ul&gt; <a name=line.36></a>
*/</span><a name=line.37></a>
<span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>class</span> ExceptionHandler {<a name=line.38></a>
<a name=line.39></a>
  <span class='comment'>/**<a name=line.40></a>
   No-argument constructor.<a name=line.41></a>
  This class must have a no-arg constructor (see class comment).<a name=line.42></a>
  */</span><a name=line.43></a>
  <span class='keyword'>public</span> ExceptionHandler(){<a name=line.44></a>
    <span class='comment'>//empty<a name=line.45></a>
</span>  }<a name=line.46></a>
  <a name=line.47></a>
  <span class='comment'>/** <a name=line.48></a>
   Custom handler for uncaught exceptions.<a name=line.49></a>
   <a name=line.50></a>
   &lt;P&gt;Displays a simple model dialog to the user, showing that an error has occured.  <a name=line.51></a>
   The text of the error includes {@link Throwable#toString()}.<a name=line.52></a>
  */</span><a name=line.53></a>
  <span class='keyword'>public</span> <span class='keyword'>void</span> handle(Throwable aThrowable){<a name=line.54></a>
    fLogger.severe(getStackTrace(aThrowable));<a name=line.55></a>
    JOptionPane.showMessageDialog(<a name=line.56></a>
      UiUtil.getActiveFrame(), <span class='literal'>"Error: "</span> + aThrowable.toString(), <a name=line.57></a>
      <span class='literal'>"Error"</span>, JOptionPane.ERROR_MESSAGE<a name=line.58></a>
    );<a name=line.59></a>
  }<a name=line.60></a>
<a name=line.61></a>
  <span class='comment'>// PRIVATE //<a name=line.62></a>
</span>  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>final</span> Logger fLogger = Util.getLogger(ExceptionHandler.<span class='keyword'>class</span>);<a name=line.63></a>
  <a name=line.64></a>
  <span class='keyword'>private</span> String getStackTrace(Throwable aThrowable) {<a name=line.65></a>
    <span class='keyword'>final</span> Writer result = <span class='keyword'>new</span> StringWriter();<a name=line.66></a>
    <span class='keyword'>final</span> PrintWriter printWriter = <span class='keyword'>new</span> PrintWriter(result);<a name=line.67></a>
    aThrowable.printStackTrace(printWriter);<a name=line.68></a>
    <span class='keyword'>return</span> result.toString();<a name=line.69></a>
  }<a name=line.70></a>
}<a name=line.71></a>
 <a name=line.72></a>
</PRE><a name=line.73></a>
 </BODY><a name=line.74></a>
<HTML><a name=line.75></a>
